package com.ruoyi.biz.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.biz.model.excel.BizWagesExcel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class WagesListener implements ReadListener<BizWagesExcel> {
    List<BizWagesExcel> dataList = new ArrayList<>();
    boolean isEnd = false;

    public WagesListener(List<BizWagesExcel> dataList) {
        this.dataList = dataList;
    }

    @Override
    public void invoke(BizWagesExcel data, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        if (data.getNo().contains("总计")) {
            isEnd = true;
        }
        if (!isEnd) {
            dataList.add(data);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("所有数据解析完成！");
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        ReadListener.super.onException(exception, context);
    }
}
